package com.example.exercise.service.impl;

import com.example.exercise.exception.BusinessException;
import com.example.exercise.exception.ExceptionCode;
import com.example.exercise.mapper.CheckMapper;
import com.example.exercise.model.*;
import com.example.exercise.service.CheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

/*
 *考勤记录
 */
@Service
public class CheckServiceImpl implements CheckService {

    @Autowired
    CheckMapper checkMapper;

    //存入考勤记录
    private Map<String, AttendanceRecord> attendanceRecordMap = new HashMap<>();

    /*
     *签到
     */
    @Override
    public AttendanceRecord register(String employeeId) {
        //检查是否已经签到
        AttendanceRecord existing = checkMapper.findByEmployeeIdAndDate(employeeId, LocalDate.now());
        //如果不为空表示已经签到l
        if (existing != null) {
            throw new BusinessException(ExceptionCode.ALREADY_SIGNED_IN);
        }

        //当前时间
        LocalDateTime now = LocalDateTime.now();
        //创建考勤记录
        AttendanceRecord attendanceRecord = new AttendanceRecord();
        attendanceRecord.setEmployeeId(employeeId);
        attendanceRecord.setSignInTime(now);

        LocalTime latestSignInTime = LocalTime.of(9, 0);
        //判断当前时间是否超过9点钟，超过就迟到，
        if (now.toLocalTime().isAfter(latestSignInTime)) {

            attendanceRecord.setSignType(SignType.LATE);//迟到状态


        } else {
            attendanceRecord.setSignType(SignType.REGISTER);//签到
        }

        //存入模拟数据库
        // attendanceRecordMap.put(attendanceRecord.getEmployeeId(), attendanceRecord);
        checkMapper.insert(attendanceRecord);
        return attendanceRecord;
    }

    //签退
    @Override
    public AttendanceRecord out(String employeeId) {
        //获得考勤记录
        AttendanceRecord attendanceRecor = checkMapper.findByEmployeeIdAndDate(employeeId, LocalDate.now());

        //考勤记录是否空，存在
        if (attendanceRecor == null) {
            throw new BusinessException(ExceptionCode.INFORMATION);
        }
        //如果有签退时间
        if (attendanceRecor.getSignOutTime() != null) {
            throw new BusinessException(ExceptionCode.INFORMATION);//已经签退
        }

        //获得签退时间
        LocalDateTime outTime = LocalDateTime.now();
        attendanceRecor.setSignOutTime(outTime);

        //计算工作时长（精确到分钟）
        Duration duration = Duration.between(attendanceRecor.getSignInTime(), outTime);
        long hour = duration.toHours();
        if (hour < 8) {//早退判断，不足八小时
            attendanceRecor.setSignType(SignType.EARLY);

        }

        //更新状态为签退
        checkMapper.update(attendanceRecor);
        return attendanceRecor;
    }

    //请假申请
    @Override
    public Vacate leave(String employeeId, LeaveType type,
                        LocalDateTime start, LocalDateTime end, String reason) {
        //创建请假申请
        Vacate vacate = new Vacate();
        vacate.setLeaveId(employeeId);
        vacate.setType(type);
        vacate.setStartTime(start);
        vacate.setEndTime(end);
        vacate.setReason(reason);

        checkMapper.insertVacate(vacate);
        return vacate;
    }

    @Override
    public void approve(String leaveId, boolean isApproved){
        Vacate vacate =checkMapper.findVacateId(leaveId);
        if(vacate==null){//是否请假
            throw new BusinessException(ExceptionCode.LEAVE_NOT_FOUND);
        }
        vacate.setStatus(isApproved ?ApprovalStatus.APPROVED :ApprovalStatus.PENDING);

        checkMapper.updateVate(vacate);
    }

}

